export default class orc_utils {
    /**
     * 讲数字number转化成string,20300 = 2.03万
     * @param number 数字
    */
    public static string_number(number:number):string
    {
        let unit = ["","K","M","G","aa","ab","ac","ad"]; 
        let unit_num = 0;
        let number_string:string;
        let temp:number;
        if(number < 1000)
        {
            temp = number;
        }
        else if(number < 1000000)
        {
            temp = number/1000;            
            
            unit_num = 1;
        }
        else if(number < 1000000000)
        {
            temp = number/1000000;  
            unit_num = 2;
        }

        if(temp>=100)
        {
            number_string = temp.toFixed(0)
        }
        else if(temp>=10)
        {
            number_string = temp.toFixed(1)
        }
        else if(temp>0)
        {
            number_string = temp.toFixed(2)
        }
        else if(temp <= 0)
        {
            number_string = '0';
        }

        number_string = number_string + unit[unit_num];
        
        return number_string;
    }

    /**
     * 将秒时间转换为 03:15这种格式
     * @param time 
     */
    public static clock_convert(time:number):string
    {        
        let hour:String = ('00' + String(Math.floor(Number(time) / 3600))).slice(-2) == '00'? '':('00' + String(Math.floor(Number(time) / 3600))).slice(-2)+":"; 
        let minus:String = '';
        if(hour == '')
        {
            minus = ('00' + String(Math.floor(Number(time) / 60 % 60 ))).slice(-2) == '00'? '':('00' + String(Math.floor(Number(time) / 60 % 60))).slice(-2) +":";
        }
        else
        {
            minus = ('00' + String(Math.floor(Number(time) / 60 % 60))).slice(-2) +":";
        }
        
        let second:String = ('00' + String(Number(time) % 60)).slice(-2);
        let clock_string:string = hour +"" + minus + "" +second;
       
        return clock_string;        
    }

    /**
     * 传入时间戳,返回日期。如果是当天，则显示几小时前 
    */
    public static time_to_data(time:any)
    {
        let time_param = Number(time);
        var timestamp = (new Date()).getTime()/1000;    //获取当前时间戳 单位为S;
        let return_date:string;
        //console.log('timestamp:',timestamp,time);
        
        if(timestamp - time >= (24 * 3600))
        {
            //隔天了
            let m =  (new Date(time_param*1000)).getMonth();//time_param.getMonth();
            let d =  (new Date(time_param*1000)).getDate();//time_param.getMonth();//time_param.getDate();
            
            return_date = String(m+1)+'月'+String(d)+'日';            
        }
        else
        {
            //没有超过24小时
            let time_cal = Math.floor((timestamp - time_param)/3600);
            if(time_cal <= 0)
            {
                return_date = '1小时前';
            }
            else if(time_cal >= 24)
            {
                return_date = '23小时前';
            }
            else
            {
                return_date =   String(time_cal) + '小时前';
            }
        }
        return return_date;
    }

    /**
     * 数组除重
     * @param arr 数组参数 
    */
    public static remove_duplicate(arr:any){        
        var newArr = [];
        for(var i = 0;i < arr.length;i ++){
            if(newArr.indexOf(arr[i]) < 0){
                newArr.push(arr[i]);
            }
        }
        return newArr;
    }

     /**
     * 中文占2个字符，英文为1个字符
     * @param nickname 用户昵称
     * @param length 显示昵称长度
     */
    public static player_nickname_length(nickname:string,length:number){
        let l =0;
   	    let nickname_easy = '';
        for(let i=0; i<nickname.length; i++) 
        { 
            if ((nickname.charCodeAt(i) & 0xff00) != 0) 
            { 
                l += 2; 
            } 
            else
            {
                l += 1; 
            }
            if(l>length*2)
            {
                break;
            }
            else
            {
                nickname_easy = nickname_easy + nickname[i];
            }            
        }
        return nickname_easy;
    }
}
